﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UserGroupData;
using AutoMapper;
using UserGroupWeb.Models;

namespace UserGroupWeb.Controllers
{
    //[Authorize]
    public class SponsorController : Controller
    {
        private ISponsorRepository _sponsorRepository;

        public SponsorController()
            : this(new SponsorRepository())
        {
        }

        public SponsorController(ISponsorRepository sponsorRepository)
        {
            _sponsorRepository = sponsorRepository;
        }

        public ActionResult Index()
        {
            var model = new SponsorIndexModel();
            model.Sponsors = _sponsorRepository.GetAllSponsors();
            return View(model);
        }

        public ActionResult Create()
        {
            var model = new SponsorFormModel();
            model.Sponsor = new SponsorDTO();
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(SponsorFormModel model)
        {
            _sponsorRepository.AddNewSponsor(model.Sponsor);
            return RedirectToAction("Index");
        }

        public ActionResult Edit(int id)
        {
            var model = new SponsorFormModel();
            model.Sponsor = _sponsorRepository.GetSponsor(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(SponsorFormModel model)
        {
            _sponsorRepository.UpdateSponsor(model.Sponsor);
            return RedirectToAction("Index");
        }

        //[HttpPost]
        //public ActionResult Delete(int id)
        //{
        //    _sponsorRepository.DeleteSponsor(id);

        //    return RedirectToAction("Index");
        //}

        public ActionResult Details(int id)
        {
            SponsorDTO sponsor = _sponsorRepository.GetSponsor(id);
            var vm = new SponsorFormModel();
            vm.Sponsor = sponsor;
            return View(vm);

            //var model = new SponsorFormModel();
            //model.Sponsor = _sponsorRepository.GetSponsor(id);
            //return View(model);
        }

        
        public ActionResult Delete(int id)
        {
            SponsorDTO sponsor = _sponsorRepository.GetSponsor(id);
            var vm = new SponsorFormModel();
            vm.Sponsor = sponsor;
            return View(vm);
        }

        //
        // POST: /PrizeFormat/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                _sponsorRepository.DeleteSponsor(id);
                return RedirectToAction("Index");
            }
            catch
            {
                return RedirectToAction("Details", id);
            }
        }
    }
}
